package com.ssm.gaode.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.net.URLEncoder;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.junit.Test;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONSerializer;
/**
 * web服务测试（有限制，超量封停）
 * @author Administrator
 * web服务key=a8376f226b863857bc5b702a50f2329a
 * 文档网址：http://lbs.amap.com/api/webservice/guide/tools/flowlevel
 */
public class GaodeTest1{

		//文档网址：http://lbs.amap.com/api/webservice/guide/api/georegeo
		//地理编码
		@Test
		public void testGet1() throws Exception{
			String str1=URLEncoder.encode("南航大厦","utf-8");
			String str2=URLEncoder.encode("深圳","utf-8");
			String url="http://restapi.amap.com/v3/geocode/geo?key=a8376f226b863857bc5b702a50f2329a&address="+str1+"&city="+str2+"";
			System.out.println(url);
			GetMethod getMethod = new GetMethod(url);
			getMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			HttpClient client = new HttpClient();
			String reqBody = null;
			int status = client.executeMethod(getMethod);
	        if (status == HttpStatus.SC_OK){
	            String result = getMethod.getResponseBodyAsString();
	        	System.out.println(result);
	        }else if (status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY){
	            Header locationHeader = getMethod.getResponseHeader("location");
	            String location = null;
	            if (locationHeader != null){
	                location = locationHeader.getValue();
	                System.out.println("The page was redirected to:" + location);
	            }else{
	                System.err.println("Location field value is null.");
	            }
	        } else{
	            System.out.println("fail======" + status);
	        }
		}
	//文档网址：http://lbs.amap.com/api/webservice/guide/api/direction
	//路径规划
	@Test
	public void testGet2() throws Exception{
		String url="http://restapi.amap.com/v3/direction/transit/integrated?origin=116.481499,39.990475&destination=116.465063,39.999538&city=010&output=json&key=a8376f226b863857bc5b702a50f2329a";
		GetMethod getMethod = new GetMethod(url);
		getMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		HttpClient client = new HttpClient();
		String reqBody = null;
		int status = client.executeMethod(getMethod);
        if (status == HttpStatus.SC_OK){
            InputStream inputStream = getMethod.getResponseBodyAsStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));  
            String line = null;  
            StringBuilder sb = new StringBuilder();  
            while ((line = br.readLine()) != null) {  
                sb.append(line);  
            }  
            reqBody = URLDecoder.decode(sb.toString(), "UTF-8");  
            reqBody = reqBody.substring(reqBody.indexOf("{"));  
            System.out.println("JsonReq reqBody>>>>>" + reqBody);
        }else if (status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY){
            Header locationHeader = getMethod.getResponseHeader("location");
            String location = null;
            if (locationHeader != null){
                location = locationHeader.getValue();
                System.out.println("The page was redirected to:" + location);
            }else{
                System.err.println("Location field value is null.");
            }
        }else{
            System.out.println("fail======" + status);
        }
	}
	
	/**
	 * 返回json解析测试
	 */
	@Test
	public void test3() {
	  //alibaba fastjson
      String json="{\"status\":\"1\",\"info\":\"OK\",\"infocode\":\"10000\",\"count\":\"1\",\"1\":[{\"formatted_address\":\"广东省深圳市南山区南航大厦\",\"province\":\"广东省\",\"citycode\":\"0755\",\"city\":\"深圳市\",\"district\":\"南山区\",\"township\":[],\"neighborhood\":{\"name\":[],\"type\":[]},\"building\":{\"name\":[],\"type\":[]},\"adcode\":\"440305\",\"street\":[],\"number\":[],\"location\":\"113.950781,22.555537\",\"level\":\"兴趣点\"}]}";

      JSONObject jsonObject = JSON.parseObject(json);   //有{}就是JSONObject
      JSONArray array = (JSONArray)jsonObject.get("geocodes"); //有[]就是JSONArray
      System.out.println(array.toString());
	    for (int i = 0; i < array.size(); i++) {
		    JSONObject jsonObject1 = (JSONObject)array.get(i); //这里i就是等于0，只有一个元素对象
		    Object object = jsonObject1.get("citycode");//里面字段什么都没有就是Object
		    System.out.println(object.toString());
	    }
	}
	@Test
	public void test4() {
		String str1 =URLEncoder.encode("北京市");
		String str2 =URLEncoder.encode("写字楼");
		String url="http://yuntuapi.amap.com/datasearch/local?tableid=5b0769957bbf1916a573e887&city="+str1+"&keywords=银行&filter=type:"+str2+"&limit=50&page=1&key=a8376f226b863857bc5b702a50f2329a";
		System.out.println(url);//5b0769957bbf1916a573e887
		String str3 =URLEncoder.encode("阜通东大街");
		String url1="http://yuntuapi.amap.com/datasearch/around?tableid=5b0769957bbf1916a573e887&keywords="+str3+"&<br>center=116.481471,39.990471&radius=500&filter=type:写字楼&limit=10&page=1<br>&key=a8376f226b863857bc5b702a50f2329a";
		System.out.println(url1);
	}
	public static void main(String[] args) throws Exception{
		String aaa="é«å¾·å°å¾";
		byte[] bytes = aaa.getBytes("iso-8859-1");
		String string = new String(bytes);
		System.out.println(string);
	}
	
}
